
from lexer import Lexer
from parser import Parser
from interpreter import Interpreter


#打印输出Lexer中的信息
def printLexer(lexer):
	token={};

	i=0;
	while 1:
		lexerInfo=lexer.get_next_token();
		token[i]={'token':'{value_type:\''+lexerInfo.value_type+'\',value:\''+str(lexerInfo.value)+'\'}'};
		i=i+1
		if lexerInfo.value_type=='EOF':
			break
	print(token)


#打印输出Parser中的抽象语法树
def printParser(lexer):
	while 1:
		parserInfo=parser.parser();
		print(parserInfo)



def main():
	# text = '1 + 3 * (10 / (12 / (3 + 1)-1))'
	# text='( 5 + 3 ) * 12 / 3'

	# text='5--2'
	# text =  '''
	# 			BEGIN
	# 				BEGIN
	# 					number := 2;
	# 					a := number;
	# 					b := 10 * a + 10 * number / 4;
	# 					c := a - - b;
	# 				END;
	# 				x := 11;
	# 			END.
	# 		''';

	text = '''
			PROGRAM Part10;
			VAR
			   number     : INTEGER;
			   a, b, c, x : INTEGER;
			   y          : REAL;
			
			BEGIN {Part10}
			   BEGIN
				  number := 2;
				  a := number;
				  b := 10 * a + 10 * number DIV 6;
				  c := a - - b
			   END;
			   x := 11;
			   y := 20 / 7 + 3.14;
			   { writeln('a = ', a); }
			   { writeln('b = ', b); }
			   { writeln('c = ', c); }
			   { writeln('number = ', number); }
			   { writeln('x = ', x); }
			   { writeln('y = ', y); }
			END.  {Part10}
		'''


	lexer = Lexer(text);

	#打印输出Lexer中的信息
	# printLexer(lexer);
	# exit();

	parser = Parser(lexer);
	tree = parser.parser()  # 获取语法分析器分析后的树对象
	# print(tree.viewTree())
	# exit();


	interpreter = Interpreter(parser)  # 实例化解释器对象
	interpreter.interpret()  # 执行运算方法获取运算结果
	print(interpreter.GLOBAL_SCOPE)  # 显示输出符号表



if __name__ == '__main__':
	main()

